<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class display_image_model extends Model
{
	function display_image_model()
	{
		parent::Model();
	}
	
	function thumbnail($id)
	{
		$q = $this->db->get_where(IMAGE, array(IMAGE_IMAGE_ID=>$id, IMAGE_FRANCHISE_ID=>$this->admin_id()));
		$row = $q->row();

		if ($row)
			return $this->full_filename($row->{IMAGE_THUMB_LOCATION});
		else
			return $this->dummy();
	}
	
	function big_thumbnail($id)
	{
		$q = $this->db->get_where(IMAGE, array(IMAGE_IMAGE_ID=>$id, IMAGE_FRANCHISE_ID=>$this->admin_id()));
		$row = $q->row();
		if ($row)
			return $this->full_filename($row->{IMAGE_LOCATION});
		else
			return $this->dummy();
	}
	
	function admin_id()
	{
		global $CI;
		$a = $CI->session->userdata('admin_auth_logged');
		return $a['admin_id'];
	}
	
	function dummy()
	{
		return APPPATH.'assets/admin/images/non.jpg';
	}
	
	function path()
	{
		return APPPATH.'assets/admin/products/1';
	}
	
	function full_filename($name)
	{
		$path = $this->path().'/'.$name;
		if ($name && file_exists($path))
			return $path;
		else
			return $this->dummy();
	}
	
	function delete_image($id)
	{
		$q = $this->db->get_where(IMAGE, array(IMAGE_IMAGE_ID=>$id, IMAGE_FRANCHISE_ID=>$this->admin_id()));
		$row = $q->row();
		if ($row)
		{
			$path = $this->full_filename($row->{IMAGE_LOCATION});
			if ($path != $this->dummy()) unlink($path);
			$path = $this->full_filename($row->{IMAGE_THUMB_LOCATION});
			if ($path != $this->dummy()) unlink($path);
		}
	}
}

?>